package RecursiveSearcher;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName LC77
 * @Description 组合
 * @Author ZJX
 * @Date 2024/10/22 13:57
 * @Version 1.0
 */

public class LC77 {
    List<List<Integer>> ret;
    List<Integer> path;

    public List<List<Integer>> combine(int n, int k) {
        ret = new ArrayList<>();
        path = new ArrayList<>();

        if (k <= 0 || n < k) {
            return ret;
        }

        dfs(n,k,1);

        return ret;
    }

    private void dfs(int n, int k, int startIndex) {
        if (path.size() == k){
            ret.add(new ArrayList<>(path));
            return ;
        }
        for (int i = startIndex; i <= n - (k - path.size()) + 1; i++) {
            path.add(i);
            dfs(n,k,i+1);
            path.remove(path.size() - 1);
        }
    }

    public static void main(String[] args) {
        LC77 solution = new LC77();

        // 测试输入：n = 4, k = 2
        List<List<Integer>> result = solution.combine(4, 2);

        // 输出结果
        System.out.println("组合结果: ");
        for (List<Integer> combination : result) {
            System.out.println(combination);
        }
    }
}
